/*-----------------------------------------------File Info------------------------------------------------
** File Name:               key.c  
** Last modified date:      2016.07.22
** Last version:            V0.1
** Description:             
**--------------------------------------------------------------------------------------------------------            
** Created date:            2016.09.07
** author:                  YLY
** Version:                 V0.1
** Descriptions:            键盘扫描处理函数
**--------------------------------------------------------------------------------------------------------
*/
/*************************************************************
* 	include 
* 	头文件	
*************************************************************/
#include "I2cWR.h"
#include "tsm12.h"
#include "key.h"

void checkTsm12(void) 
{
	uint8_t regBuf[1];
	uint8_t ret = 0;
	do
	{
		//CTRL2 default 0x01 
		if(i2cReadReg(TSM12S_ADDR,CTRL2,regBuf,1) == FAIL)
		{
			ret = 1;
			tsm12Init();
			break;		//错误
		}
		//TSDBG("CTRL2:%x\n",regBuf[0]);
		if(regBuf[0] == 0x01)	//未初始化,初始化后，最后两位是11
		{
			ret = 2;
			tsm12Init();
		}
	}while(0);
	
	if(ret)
		TSERR("TsmERR=%d\n",ret);
}

/*******************************************************************************
*名 称: TSM12_Init()
*功 能: TSM12M 功能初始化
*入口参数: 无
*出口参数: 无
*说 明:
*******************************************************************************/
bool tsm12Init(void)
{
	uint8_t regBuf[1];
	uint8_t ret = 0;
	do
	{	
		if(i2cReadReg(TSM12S_ADDR,CTRL2,regBuf,1) == FAIL)
		{
			ret = 1;
			break;	
		}
		
		regBuf[0] = 0x0B;
		if(i2cWriteReg(TSM12S_ADDR,CTRL2,regBuf,1) == FAIL) // Value of 09H, Set the Software reset.
		{
			ret = 2;
			break;	
		}
		/* ---- Low Sensitivity : 0xFF, High Sensitivity 0x00 -------*/
		/* a,b 表示 Ch_n 和 CH_n-1 的灵敏度配置，
			取值范围分二段：
			1. 0 ~ 7(灵敏度逐渐降低，只能用十六进制表示)
			2. 9 ~ F(灵敏度逐渐降低，只能用十六进制表示)
			第 2 段灵敏度整体高于第 1 段灵敏度
		*/
		regBuf[0] = CH_SENS(4,4);
		if(i2cWriteReg(TSM12S_ADDR, SENS1,regBuf,1) == FAIL) // ----------- Sensivity Register Ch 1 ~ Ch2 ----------------
		{
			ret = 3;
			break;	
		}		
		if(i2cWriteReg(TSM12S_ADDR, SENS2,regBuf,1) == FAIL) // ----------- Sensivity Register Ch 3 ~ Ch4 ----------------
		{
			ret = 4;
			break;	
		}		
		if(i2cWriteReg(TSM12S_ADDR, SENS3,regBuf,1) == FAIL) // ----------- Sensivity Register Ch 5 ~ Ch 6 ----------------
		{
			ret = 5;
			break;	
		}		
		if(i2cWriteReg(TSM12S_ADDR, SENS4,regBuf,1) == FAIL) // ----------- Sensivity Register Ch 7 ~ Ch8 ---------------
		{
			ret = 6;
			break;
		}		
		if(i2cWriteReg(TSM12S_ADDR, SENS5,regBuf,1) == FAIL) // ----------- Sensivity Register Ch 9 ~ Ch10 ---------------
		{
			ret = 7;
			break;
		}		
		if(i2cWriteReg(TSM12S_ADDR, SENS6,regBuf,1) == FAIL) // ----------- Sensivity Register Ch 11 ~ Ch12 ---------------
		{
			ret = 8;
			break;
		}			
		/* General Control Register1 setting */  
		regBuf[0] = 0x22;
		if(i2cWriteReg(TSM12S_ADDR, CTRL1,regBuf,1) == FAIL) //TSM_Write_Byte(CTRL1, 0x33);// ----------- General Control Register ---------------------
		{
			ret = 9;
			break;
		}
		// ----------- Reference Reset Control Register 1 -------------	
		regBuf[0] = 0xF0;
		if(i2cWriteReg(TSM12S_ADDR, REF_RST1,regBuf,1) == FAIL) //TSM_Write_2Byte(REF_RST1, 0x00, 0x00);//renew value of everyone channel
		{
			ret = 10;
			break;
		}
		// ----------- Reference Reset Control Register 2 -------------	
		regBuf[0] = 0x00;
		if(i2cWriteReg(TSM12S_ADDR, REF_RST2,regBuf,1) == FAIL) //TSM_Write_2Byte(REF_RST1, 0x00, 0x00);//renew value of everyone channel
		{
			ret = 11;
			break;
		}		
		// ---------- Channel On/Off Control Register (Ch1 ~Ch4)------
		// Bit "0" = Chnael ON , Bit "1" = Chnael OFF
		regBuf[0] = 0xF0;
		if(i2cWriteReg(TSM12S_ADDR, CH_HOLD1,regBuf,1) == FAIL) //TSM_Write_2Byte(CH_HOLD1, 0x00, 0x00);//enable all channels
		{
			ret = 12;
			break;
		}
		regBuf[0] = 0x00;
		if(i2cWriteReg(TSM12S_ADDR, CH_HOLD2,regBuf,1) == FAIL) 
		{
			ret = 13;
			break;
		}	
		regBuf[0] = 0x00;
		if(i2cWriteReg(TSM12S_ADDR, CAL_HOLD1,regBuf,1) == FAIL)
		{
			ret = 14;
			break;
		}
		regBuf[0] = 0x00;
		if(i2cWriteReg(TSM12S_ADDR, CAL_HOLD2,regBuf,1) == FAIL)
		{
			ret = 15;
			break;
		}	
		// -------- Software Reset Clear----- 
		regBuf[0] = 0x03;
		if(i2cWriteReg(TSM12S_ADDR, CTRL2,regBuf,1) == FAIL)// Value of 09H, Release the Software reset.
		{
			ret = 16;
			break;
		}									//指定中断函数	
	}while(0);
	
	if(ret)
	{
		TSERR("TsmERR=%d\n",ret);
		return false;
	}		
	return true;
}

uint8_t readTsm12Value(uint8_t *pBuf)
{
	return i2cReadReg(TSM12S_ADDR, 0x10, pBuf, 4);
}








